//
//  LLFirstViewViewController.m
//  LoveLove
//
//  Created by wkf on 2019/3/27.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLFirstViewViewController.h"
#import "LLHomeTableViewController.h"
#import "LLChannelShowView.h"
#import "LLChannelModel.h"
#import "LLSideModel.h"
#import "LLChannelModel.h"

@interface LLFirstViewViewController ()<LLChannelShowViewDelegate, XYSearchViewDelegate>
@property (strong, nonatomic) NSMutableArray *titleArr;
@property (strong, nonatomic) LLChannelShowView *channelShowView;
@property (copy, nonatomic) NSArray *channelModels;
@property (strong, nonatomic) LLButton *addBtn;
@property (strong, nonatomic) NSArray *sideModels;
@property (nonatomic,strong) LLHomeTableViewController *homevc;
//y头部标签
@property (nonatomic,strong) NSMutableArray *tagArray;
@property (nonatomic,strong) NSMutableArray *colArray;

@end

@implementation LLFirstViewViewController

-(NSMutableArray *)tagArray{
    if (!_tagArray) {

        LLChannelModel *model1 = [LLChannelModel new];
        model1.interest_name = @"热门";
        model1.interest_id = @"-1";

        LLChannelModel *model2 = [LLChannelModel new];
        model2.interest_name = @"关注";
        model2.interest_id = @"0";
   

        _tagArray = [NSMutableArray arrayWithObjects:model1, model2,nil];
       
    }
    return _tagArray;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initVCSetting];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initNavigationBar];
    [self initAddBtn];

  
    self.colArray = [NSMutableArray array];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.tintColor = UIColor.whiteColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:LLRegularFont(18)}];
    self.navigationController.navigationBar.barTintColor = LLColor(218, 13, 13);
}

- (void)initNavigationBar {

    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    image.image = [UIImage imageNamed:@"logo11"];
    image.contentMode = UIViewContentModeScaleAspectFit;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:image];

    XYSearchView *searchView = [[XYSearchView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth-120, 30) isTap:YES];
    searchView.placeholder = @"请输入搜索内容";
    searchView.delegate = self;
    self.navigationItem.titleView = searchView;
}

- (void)initAddBtn {
    _addBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"add" titleColor:nil font:0];
    _addBtn.frame = CGRectMake(LLScreenWidth-50, 0, 50, 50);
    [_addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_addBtn];
}

- (void) leftBarButtonItemClick
{

}

- (void)initVCSetting {
    self.selectIndex = 0;
    self.menuViewStyle = WMMenuViewStyleLine;
    self.titleColorNormal = LLColorRGB(67);
    self.titleSizeNormal = 16;
    self.titleSizeSelected = 16;
    self.titleColorSelected = UIColor.blackColor;
    self.automaticallyCalculatesItemWidths = YES;
    self.progressViewIsNaughty = YES;
    self.progressWidth = 15;
    self.progressHeight = 3;
    self.progressColor = LLColor(225, 65, 52);
    self.progressViewCornerRadius = 1.5;
}

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.titleArr.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return self.titleArr[index];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index
{
    _homevc = LLHomeTableViewController.new;
    _homevc.index = index;
    _homevc.news_type = 0;

    LLChannelModel *model = self.tagArray[index];
    _homevc.interest_id = model.interest_id;

    return _homevc;
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
    CGFloat width = [super menuView:menu widthForItemAtIndex:index];
    return width + 20;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
//    CGFloat leftMargin = self.showOnNavigationBar ? 50 : 0;   `
//    CGFloat originY = self.showOnNavigationBar ? 0 : CGRectGetMaxY(self.navigationController.navigationBar.frame);

    return CGRectMake(0, 0, self.view.frame.size.width-50, 50);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, self.view.frame.size.width, self.view.frame.size.height - originY);
}

#pragma mark - LLChannelShowViewDelegate

- (void)didClickChannelIndex:(NSInteger)index {
    NSLog(@"index--%ld",index);
}

-(void)updateView:(NSArray *)tagArray{
//    self.channelShowView  = nil;
    [self.tagArray removeAllObjects];
    [self.titleArr removeAllObjects];
    self.tagArray = nil;
    self.titleArr = nil;
    
//    LLChannelModel *model1 = [LLChannelModel new];
//    model1.interest_name = @"热门";
//    model1.interest_id = @"-1";
//
//    LLChannelModel *model2 = [LLChannelModel new];
//    model2.interest_name = @"关注";
//    model2.interest_id = @"0";
    

    
    [self reloadData];
   
}
#pragma mark - private method

//- (void)leftBarButtonItemClick {
//    [self.sideView show];
//}

- (void)addBtnClick {
    [self.channelShowView show];
}

#pragma mark - lazy load

- (NSMutableArray *)titleArr {
    if (!_titleArr) {
        NSArray *array = [LLUserDefaults objectForKey:@"user_interest"];
        NSArray *resultArr = [NSArray yy_modelArrayWithClass:[LLChannelModel class] json:array];
        
        NSMutableArray *arr = [NSMutableArray array];
        for (LLChannelModel *model in resultArr) {
            [arr addObject:model.interest_name];
        }

        LLChannelModel *model1 = [LLChannelModel new];
        model1.interest_name = @"热门";
        model1.interest_id = @"-1";

        LLChannelModel *model2 = [LLChannelModel new];
        model2.interest_name = @"关注";
        model2.interest_id = @"0";

        [self.tagArray addObject:model1];
        [self.tagArray addObject:model2];
        [self.tagArray addObjectsFromArray:resultArr];
        _titleArr = [[NSMutableArray alloc] initWithArray:@[model1.interest_name,model2.interest_name]];
        [_titleArr addObjectsFromArray:arr];
    }
    return _titleArr;
}

- (LLChannelShowView *)channelShowView {
    if (!_channelShowView) {
        _channelShowView = [[LLChannelShowView alloc] initWithFrame:CGRectMake(0, LLScreenHeight, LLScreenWidth, LLScreenHeight)];
        _channelShowView.backgroundColor = UIColor.whiteColor;
        _channelShowView.models = self.channelModels;
        _channelShowView.delegate = self;
        [self.tabBarController.view addSubview:_channelShowView];
    }
    return _channelShowView;
}

- (NSArray *)channelModels {
    if (!_channelModels) {
         NSArray *array = [LLUserDefaults objectForKey:@"all_user_interest"];
        NSArray *resultArr = [NSArray yy_modelArrayWithClass:[LLChannelModel class] json:array];
        _channelModels = [NSMutableArray arrayWithArray:resultArr];
    }
    return _channelModels;
}

//- (LLSideView *)sideView {
//    if (!_sideView) {
//        _sideView = [[LLSideView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLScreenHeight)];
//        _sideView.models = self.sideModels;
//        [self.tabBarController.view addSubview:_sideView];
//    }
//    return _sideView;
//}

- (NSArray *)sideModels {
    if (!_sideModels) {
        NSMutableArray *mutableArr = [NSMutableArray array];
        for (NSString *title in @[@"幼儿园",@"小学",@"初中",@"高中",@"大学"]) {
            LLSideModel *model = LLSideModel.new;
            model.title = title;
            model.isSelected = @"0";
            [mutableArr addObject:model];
        }
        _sideModels = mutableArr;
    }
    return _sideModels;
}

- (void) didSearch:(NSString *)search_text
{
    _homevc.search_text = search_text;
}

@end
